package com.itheima.service.impl;

import com.itheima.Utils.redisUtil;
import com.itheima.dao.SetMealDao;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.SetMeal;
import com.itheima.service.SetMealService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import redis.clients.jedis.JedisPool;

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private JedisPool jedisPool;
    @Autowired
    private SetMealDao setMealDao;
    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;
    @Value("${stackpage_path}")
    private String staticPageName;

    @Override
    public PageInfo pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        PageHelper.startPage(currentPage, pageSize);
        List<SetMeal> list = setMealDao.findByCondition(queryPageBean.getQueryString());
        return new PageInfo(list);
    }

    @Override
    public void add(SetMeal setMeal, Integer[] checkgroupIds) {
        setMealDao.add(setMeal);
        Integer smId = setMeal.getId();
        for (Integer checkgroupId : checkgroupIds) {
            Map<String, Integer> map = new HashMap<>();
            map.put("setmeal_id", smId);
            map.put("checkgroup_id", checkgroupId);
            setMealDao.setCheckGroupsAndSetMeal(map);
        }
        if (setMeal.getImg() != null) {
            jedisPool.getResource().sadd(redisUtil.RedisConstant.FOLDE_MYSQL, setMeal.getImg());
        }

        generateStaticHtml();
    }

    @Override
    public void update(SetMeal setMeal, Integer[] checkgroupIds) {
        //修改套餐表
        setMealDao.update(setMeal);
        Integer smId = setMeal.getId();
        //删除中间表关联
        setMealDao.deleteSetMeal_CheckGroups(smId);
        //添加关联
        for (Integer checkgroupId : checkgroupIds) {
            Map<String, Integer> map = new HashMap<>();
            map.put("setmeal_id", smId);
            map.put("checkgroup_id", checkgroupId);
            setMealDao.setCheckGroupsAndSetMeal(map);
        }
        if (setMeal.getImg() != null) {
            jedisPool.getResource().sadd(redisUtil.RedisConstant.FOLDE_MYSQL, setMeal.getImg());
        }

        generateStaticHtml();
    }

    /**
     * 查询套餐预约占比数据
     *
     * @return setMealCount 和 setMealNames
     */
    @Override
    public List<Map<String, Object>> findSetMealCount() {
        return setMealDao.findSetMealCount();
    }

    private void generateStaticHtml() {
        List<SetMeal> list = setMealDao.findAll();
        generateMobileSetmealListHtml(list);

        generateMobileSetmealDetailHtml(list);
    }

    private void generateMobileSetmealDetailHtml(List<SetMeal> list) {
        for (SetMeal setMeal : list) {
            Map map = new HashMap();
            SetMeal detail = setMealDao.findById(setMeal.getId());
            map.put("setmeal", detail);
            generateHtml("mobile_setmeal_detail.ftl", "setmeal_detail_" + setMeal.getId() + ".html", map);
        }
    }

    private void generateMobileSetmealListHtml(List<SetMeal> list) {
        Map map = new HashMap();
        map.put("setMealList", list);
        generateHtml("mobile_setmeal.ftl", "m_setmeal.html", map);
    }

    public void generateHtml(String templateName, String mobilePageName, Map map) {
        Configuration configuration = freeMarkerConfigurer.getConfiguration();
        Writer out = null;
        try {
            Template template = configuration.getTemplate(templateName);
            out = new FileWriter(new File(staticPageName + "/" + mobilePageName));
            template.process(map, out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void delete(Integer smId) {
        setMealDao.deleteSetMeal_CheckGroups(smId);
        setMealDao.deleteById(smId);
    }

    @Override
    public List<SetMeal> findAll() {
        return setMealDao.findAll();
    }

    @Override
    public SetMeal findById(Integer id) {
        SetMeal setMeal = setMealDao.findById(id);
        return setMeal;
    }

    @Override
    public SetMeal getSetMeal(Integer id) {
        SetMeal setMeal = setMealDao.getSetMeal(id);
        return setMeal;
    }

}
